package com.weibiao.zhuhai.concurrent.syncronized;

public class CounterThread extends Thread{
	private Counter counter;
	public CounterThread(Counter counter) {
		this.counter = counter;
	}
	@Override
	public void run() {
		for (int i = 0; i < 1000; i++) {
			counter.incr();
		}
	}
	public static void main(String[] args) throws InterruptedException {
		int num = 1000;
		Counter counter = new Counter();
		Thread[] threads=new Thread[num];
		for (int i = 0; i < threads.length; i++) {
			threads[i] = new CounterThread(counter);
			threads[i].start();
		}
		for (int i = 0; i < threads.length; i++) {
			threads[i].join();
		}
		
		System.out.println(counter.getCount());
		
	}
}

class Counter {
	private int count;
	private Object lock = new Object();
	public  void incr(){
		synchronized (lock) {
			count++;
		}
	}
	public int getCount(){
//		synchronized (lock) {
			return count;
//		}
	}
}